package com.lepu.app.fun.mother_circle;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.app.application.MyApplication;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.ObserverManager;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.bean.BeanMotherCircle;
import com.lepu.app.fun.mother_circle.fragment.FragmentMotherCircle;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class MotherCircleDetailActivity extends BaseFragmentActivity implements
	OnTopbarNewLeftLayoutListener , AsyncRequest{
	private BeanMotherCircle mMotherCircle;

	private String REQUEST_CIRCLE_EXIT = "request_circle_exit";
	private String REQUEST_CIRCLE_ADD = "request_circle_add";
	private static final int MSG_CIRCLE_ADD_SUCESS = 11;
	private static final int MSG_CIRCLE_ADD_FAIL = 12;
	private static final int MSG_CIRCLE_EXIT_SUCESS = 13;
	private static final int MSG_CIRCLE_EXIT_FAIL = 14;
	
	private ImageButton mExitButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mother_circle_detail_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		Bundle data = this.getIntent().getExtras();
		if(data != null && data.size() > 0)
		{
			mMotherCircle=(BeanMotherCircle)data.getSerializable("motherCircle");
		}

		if(mMotherCircle == null)
		{
			return;
		}
		
		topbar.setTopbarTitle(mMotherCircle.cricleName);
		
		ImageView avatar = (ImageView) findViewById(R.id.circle_detail_avatar);
		TextView title = (TextView) findViewById(R.id.circle_detail_title);
		TextView headContent = (TextView) findViewById(R.id.circle_detail_head_content);
		TextView people = (TextView) findViewById(R.id.peopleCountTextView);
		TextView topic = (TextView) findViewById(R.id.topicCountTextView);
		TextView content = (TextView) findViewById(R.id.circle_detail_content);
		mExitButton = (ImageButton) findViewById(R.id.circle_detail_exit);
		
		UniversalImageLoadTool.disPlay(mMotherCircle.cricleAvatar, avatar, R.drawable.default_icon_small);
		title.setText(mMotherCircle.cricleName);
		headContent.setText(mMotherCircle.cricleContent);
		people.setText(mMotherCircle.criclePeopleCount+" 加入");
		topic.setText(mMotherCircle.cricleTopicCount+" 话题");
		content.setText(mMotherCircle.cricleRule);
		
		if(mMotherCircle.cricleType == 0){
			mExitButton.setBackgroundResource(R.drawable.mother_circle_add_selector);
		}else if(mMotherCircle.cricleType == 1){
			mExitButton.setBackgroundResource(R.drawable.mother_circle_exit_selector);
		}
		
		mExitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				boolean flagLogin = MainTabActivity.jumpLogin(MotherCircleDetailActivity.this);
				
				if(flagLogin){
					return;
				}
				
				if(mMotherCircle.cricleType == 0){
					requestAddCircle(mMotherCircle.cricleID);
				}else if(mMotherCircle.cricleType == 1){
					
				     new MyDialog(MotherCircleDetailActivity.this)
                     .setMessage("您确定退出该圈子吗?")
                     .setNegativeButton(R.string.app_cancel,
                             new View.OnClickListener() {

                                 @Override
                                 public void onClick(View v) {

                                 }
                             })
                     .setPositiveButton(R.string.app_ok,
                             new View.OnClickListener() {

                                 @Override
                                 public void onClick(View v) {
                                	 requestExitCircle(mMotherCircle.cricleID);
                                    
                                 }
                             }).create(null).show();
				     
				     
				}
			}
		});
	}
	
	public void requestExitCircle(String circleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			
			if (MyApplication.getInstance().checkIsLogin()) {
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
				params.put("UserID", loginInfo.UserID);
				params.put("LoginToken", loginInfo.LoginToken);
			} else {
				params.put("UserID", -1);
				params.put("LoginToken", "");
			}
			
			params.put("CategoryID", circleId);

		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post(Setting.getCircleExitUrl(), map, null, this,REQUEST_CIRCLE_EXIT, true,true,false);
		showProgressDialog();
	}
	
	public void requestAddCircle(String circleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			
			if (MyApplication.getInstance().checkIsLogin()) {
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
				params.put("UserID", loginInfo.UserID);
				params.put("LoginToken", loginInfo.LoginToken);
			} else {
				params.put("UserID", -1);
				params.put("LoginToken", "");
			}
			
			params.put("CategoryID", circleId);

		} catch (Exception e) {
		}
		map.put("ht", params);
		
		ApiClient.http_post(Setting.getCircleAddUrl(), map, null, this,REQUEST_CIRCLE_ADD, true, true, false);
	    showProgressDialog();
	}
	
	@SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_CIRCLE_EXIT_SUCESS:
				hideProgressDialog();
				
				FragmentMotherCircle.getInstance().request();
				ObserverManager.getInstance().notify(ObserverConst.NOTIFY_MOTHER_CIRCLE_ALL_CHANGE, null, null);
				mMotherCircle.cricleType = 0;
				mExitButton.setBackgroundResource(R.drawable.mother_circle_add_selector);
				break;

			case MSG_CIRCLE_EXIT_FAIL:
				hideProgressDialog();
				break;
				
			case MSG_CIRCLE_ADD_SUCESS:
				hideProgressDialog();
				
				FragmentMotherCircle.getInstance().request();
				ObserverManager.getInstance().notify(ObserverConst.NOTIFY_MOTHER_CIRCLE_ALL_CHANGE, null, null);
				
				mMotherCircle.cricleType = 1;
				mExitButton.setBackgroundResource(R.drawable.mother_circle_exit_selector);
				break;
				
			case MSG_CIRCLE_ADD_FAIL:
				hideProgressDialog();
				break;
				
			default:
				break;
			}
		};
	};
	
	@Override
	public void RequestComplete(Object object, Object data) {
		if(object.equals(REQUEST_CIRCLE_EXIT)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_CIRCLE_EXIT_SUCESS;
			msg.obj = (String) data;
			mHandler.sendMessage(msg);
		}else if(object.equals(REQUEST_CIRCLE_ADD)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_CIRCLE_ADD_SUCESS;
			msg.obj = (String) data;
			mHandler.sendMessage(msg);
		}

	}

	@Override
	public void RequestError(Object object, int arg1, String arg2) {
		if(object.equals(REQUEST_CIRCLE_EXIT)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_CIRCLE_EXIT_FAIL;
			mHandler.sendMessage(msg);
		}else if(object.equals(REQUEST_CIRCLE_ADD)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_CIRCLE_ADD_FAIL;
			mHandler.sendMessage(msg);
		}
		
	}

}
